import {show,adoptPet,getPetByIdAndCanAdopt,getPetList,getPetListByPet} from '@/api/pet/pets.js'
import {getPetListByStatus,addLostPet} from '@/api/pet/lost.js'

import {defineStore} from "pinia";
import Pet = API.Pet;
import {useRouter} from "vue-router";
import PetLost = API.PetLost;



const router = useRouter()



const usePetStore = defineStore(
    'pet',
    {
        state: () => ({
            showPetList: [] as Pet[],
            petList: [] as Pet[],
            pet:{} as Pet,
            petLostList:[] as PetLost[]

        }),
        getters: {

            PetInfo(id) {

            }

        },
        actions: {


            //新增寻宠启示
            addLostPet(lostPet:API.PetLost){
                return new Promise(async (resolve, reject) => {
                    console.log("=============addPetList()")
                    console.log(lostPet)
                    await addLostPet(lostPet)
                    getPetListByStatus(1).then((res: any) => {
                        this.petLostList = res.rows
                        console.log(res)
                        resolve()
                    }).catch((error: any) => {
                        reject(error)
                    })
                })
            },



            //获取寻宠启示的宠物信息    状态，1-收容审批中，2-已收容，3-领养审批中，4-已被领养，5宠物丢失
            getLostPetList(){
                return new Promise((resolve, reject) => {
                    // console.log("=============getLostPetList()")
                    getPetListByStatus(1).then((res: any) => {
                        // console.log("==========================================================getPetListLost")
                        this.petLostList = res.rows
                        console.log( res.rows)
                        console.log(this.petLostList)
                        resolve()
                    }).catch((error: any) => {
                        reject(error)
                    })
                })
            },


            getPetListByPet(pet:API.Pet){
                return new Promise((resolve, reject) => {
                    // console.log("=============getPetListByPet()")
                    console.log(pet)
                    getPetListByPet(pet).then((res: any) => {
                        this.petList = res.rows
                        resolve()
                    }).catch((error: any) => {
                        reject(error)
                    })
                })
            },

            getPetList(){
                return new Promise((resolve, reject) => {
                    // console.log("=============getPetList()")
                    getPetList().then((res: any) => {
                        // console.log(res)
                        this.petList = res.rows
                        resolve()
                    }).catch((error: any) => {
                        reject(error)
                        console.log(error)
                    })
                })

            },


            //首页查询
            async show() {
                return new Promise((resolve, reject) => {
                    show().then((res: any) => {
                        this.showPetList = res.rows
                        resolve()
                    }).catch((error: any) => {
                        reject(error)
                        console.log(error)
                    })
                })
            },
            //宠物详情页面，根据路由id查询 宠物信息   如果宠物可以被领养
            getPetInfoById(id: Number) {
                return new Promise((resolve, reject) => {
                    getPetByIdAndCanAdopt(id).then((res: any) => {
                            this.pet = res.data
                        resolve()
                    }).catch((error: any) => {
                        reject(error)
                        console.log(error)
                    })
                })
            },

            //用户领养功能
            adopt(adopt:API.adopt){
                return new Promise((resolve, reject) => {
                    adoptPet(adopt).then((res: any) => {

                        resolve()
                    }).catch((error: any) => {
                        reject(error)

                    })
                })
            }



        },

    })



export default usePetStore
